package io.ionic.starter.keep;

import android.annotation.TargetApi;
import android.app.Service;
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.Message;


import com.marianhello.bgloc.LocationService;

import io.ionic.starter.MainActivity;

/**
 * JobService
 */
@TargetApi(21)
public class AliveJobService extends JobService {
  private volatile static Service mKeepAliveService = null;

  public static boolean isJobServiceAlive() {
    return mKeepAliveService != null;
  }

  private static final int MESSAGE_ID_TASK = 0x01;
  private Handler mHandler = new Handler(new Handler.Callback() {
    @Override
    public boolean handleMessage(Message msg) {
      // 具体任务逻辑
      if (SystemUtils.isAPPALive(getApplicationContext(), Contants.PACKAGE_NAME)) {
        //Toast.makeText(getApplicationContext(), "APP活着的", Toast.LENGTH_SHORT).show();
      } else {
        Intent intent = new Intent(getApplicationContext(), MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        //Toast.makeText(getApplicationContext(), "APP被杀死，重启...", Toast.LENGTH_SHORT).show();
        /////////////
        Intent locationServiceIntent = new Intent(getApplicationContext(), LocationService.class);
        // start service to keep service running even if no clients are bound to it
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
          startForegroundService(locationServiceIntent);
        } else {
          startService(locationServiceIntent);
        }
      }
      // 通知系统任务执行结束
      jobFinished((JobParameters) msg.obj, false);
      return true;
    }
  });

  @Override
  public boolean onStartJob(JobParameters params) {
    //if (Contants.DEBUG)
    mKeepAliveService = this;
    // 返回false，系统假设这个方法返回时任务已经执行完毕；
    // 返回true，系统假定这个任务正要被执行
    Message msg = Message.obtain(mHandler, MESSAGE_ID_TASK, params);
    mHandler.sendMessage(msg);
    return true;
  }

  @Override
  public boolean onStopJob(JobParameters params) {
    mHandler.removeMessages(MESSAGE_ID_TASK);
    return false;
  }
}
